﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using Xiaov.SmartWeb.Model;

namespace Xiaov.SmartWeb.Application.TZGC
{
    public partial class GCSelect : BasePage
    {
        private string _moduleid;
        public string ModuleID
        {
            get
            {
                if (Request.QueryString["mid"] != null)
                {
                    _moduleid = Request.QueryString["mid"].ToString();
                }
                else
                {
                    _moduleid = "0000";
                }
                return _moduleid;
            }
        }

      

        protected void Page_Load(object sender, EventArgs e)
        {
            PM_Filter();
            if (!IsPostBack)
            {
                //绑定年度
                BindND();
                //绑定工程列表
                BindGridGC();
                //绑定项目列表
                BindXM();
            }
        }

        //绑定年度下拉框
        protected void BindND()
        {
            int year_cur = DateTime.Now.Year;
            int month_cur = DateTime.Now.Month;

            for (int i = year_cur - 10; i <= year_cur + 10; i++)
            {
                DropDownList1.Items.Add(i.ToString());
            }
            DropDownList1.SelectedValue = year_cur.ToString();
            //DropDownList2.SelectedValue = month_cur.ToString();
        }

        //绑定工程GRID
        protected void BindGridGC()
        {
            StringBuilder sql_query = new StringBuilder();
            sql_query.Append("select rownum,a.gcbm,a.gcmc,a.gcnd,a.gcyd,a.ssxmbm,b.xmmc,a.sx,a.sstzxmmc,a.rowid from yt_gcb a,yt_xmb b where a.ssxmbm = b.xmbm(+)  ");
            sql_query.Append(" and (ssxmbm is null or ssxmbm = 'NOTHING')");
            int nd, yd;

            if (CheckBox1.Checked == true)
            {
                nd = Convert.ToInt32(DropDownList1.SelectedItem.ToString());
                sql_query.Append(" and a.gcnd = " + nd + " ");
            }

            if (CheckBox2.Checked == true)
            {
                yd = Convert.ToInt32(DropDownList2.SelectedItem.ToString());
                sql_query.Append(" and a.gcyd = " + yd + "  ");
            }

            sql_query.Append(" order by a.sx asc");
            GridViewGC.DataSource = Maticsoft.DBUtility.DbHelperOra.Query(sql_query.ToString());
            GridViewGC.DataBind();
        }


        //绑定项目下拉框
        protected void BindXM()
        {
            StringBuilder sql_query = new StringBuilder();
            sql_query.Append("select a.xmbm,a.xmmc from yt_xmb a where 1 = 1 ");

            int nd, yd;
            if (CheckBox1.Checked == true)
            {
                nd = Convert.ToInt32(DropDownList1.SelectedItem.ToString());
                sql_query.Append(" and a.xmnd = " + nd + " ");
            }

            if (CheckBox2.Checked == true)
            {
                yd = Convert.ToInt32(DropDownList2.SelectedItem.ToString());
                sql_query.Append(" and a.xmyd = " + yd + "  ");
            }

            DropDownList3.DataSource = Maticsoft.DBUtility.DbHelperOra.Query(sql_query.ToString());
            DropDownList3.DataTextField = "xmmc";
            DropDownList3.DataValueField = "xmbm";
            DropDownList3.DataBind();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridGC();
            BindXM();
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindGridGC();
            BindXM();
        }

        protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            BindGridGC();
            BindXM();
        }

        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            BindGridGC();
            BindXM();
        }

        protected void GridViewGC_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewGC.PageIndex = e.NewPageIndex;//分页BindData();
            BindGridGC();
        }

        protected void GridViewGC_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header)
            {
                e.Row.Cells[2].Visible = false;
                e.Row.Cells[4].Visible = false;
                e.Row.Cells[5].Visible = false;
                e.Row.Cells[6].Visible = false;
                e.Row.Cells[7].Visible = false;
                e.Row.Cells[8].Visible = false;
                e.Row.Cells[9].Visible = false;
                e.Row.Cells[10].Visible = false;
            } 
        }

        protected void GridViewGC_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //鼠标移动到某行上，该行变色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#dddddd'");
                //鼠标移开后，恢复
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
                //e.Row.Cells[1].Text = (e.Row.RowIndex + 1).ToString();
            }
        }


        //将选中工程的所属项目更新为下拉框中的项目
        protected void Button2_Click(object sender, EventArgs e)
        {            
            foreach (GridViewRow row in GridViewGC.Rows)
            {
                CheckBox cbox = (CheckBox)row.FindControl("cbxId");
                StringBuilder sql_update = new StringBuilder();                   
                if (cbox.Checked == true)
                {
                    sql_update.Append("update yt_gcb set ");  
                    sql_update.Append(" ssxmbm = '" + DropDownList3.SelectedValue.ToString().Trim() + "'");
                    sql_update.Append(" where rowid = '" + row.Cells[10].Text.ToString().Trim() + "'");
                    Maticsoft.DBUtility.DbHelperOra.ExecuteSql(sql_update.ToString());
                }
               
            }            

            //保存之后必须要重新绑定工程GRID，因为工程的编码及所属项目都变了，再检索就检索不到了
            BindGridGC();
            
        }

        
    }
}